package com.ruoyi.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.config.manager.domain.MgIdentity;
import com.ruoyi.config.manager.service.IMgIdentityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 身份Controller
 * 
 * @author ruoyi
 * @date 2023-12-20
 */
@RestController
@RequestMapping("/configManager/identity")
public class MgIdentityController extends BaseController
{
    @Autowired
    private IMgIdentityService mgIdentityService;

    /**
     * 查询身份列表
     */
    @PreAuthorize("@ss.hasPermi('configManager:identity:list')")
    @GetMapping("/list")
    public TableDataInfo list(MgIdentity mgIdentity)
    {
        startPage();
        List<MgIdentity> list = mgIdentityService.selectMgIdentityList(mgIdentity);
        return getDataTable(list);
    }
    
    /**
     * 查询已开启的身份列表
     */
    @PostMapping("/listIdentitys")
    public AjaxResult listIdentitys()
    {
        return AjaxResult.success( mgIdentityService.getIdentitysByEnabled());
    }
    

    /**
     * 导出身份列表
     */
    @PreAuthorize("@ss.hasPermi('configManager:identity:export')")
    @Log(title = "身份", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MgIdentity mgIdentity)
    {
        List<MgIdentity> list = mgIdentityService.selectMgIdentityList(mgIdentity);
        ExcelUtil<MgIdentity> util = new ExcelUtil<MgIdentity>(MgIdentity.class);
        util.exportExcel(response, list, "身份数据");
    }

    
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) {
        ExcelUtil<MgIdentity> util = new ExcelUtil<MgIdentity>(MgIdentity.class);
        try {
            List<MgIdentity> mgIdentities = util.importExcel(file.getInputStream());
            mgIdentityService.insertMgIdentitys(mgIdentities);
            System.err.println(mgIdentities.toString());
            return AjaxResult.success("导入成功");
        } catch (IOException e) {
            AjaxResult.error();
        }
        return AjaxResult.success();
    }
    
    /**
     * 获取身份详细信息
     */
    @PreAuthorize("@ss.hasPermi('configManager:identity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(mgIdentityService.selectMgIdentityById(id));
    }

    /**
     * 新增身份
     */
    @PreAuthorize("@ss.hasPermi('configManager:identity:add')")
    @Log(title = "身份", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MgIdentity mgIdentity)
    {
        return toAjax(mgIdentityService.insertMgIdentity(mgIdentity));
    }

    
    /**
     * 修改身份
     */
    @PreAuthorize("@ss.hasPermi('configManager:identity:edit')")
    @Log(title = "身份", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MgIdentity mgIdentity)
    {
        return toAjax(mgIdentityService.updateMgIdentity(mgIdentity));
    }

    /**
     * 删除身份
     */
    @PreAuthorize("@ss.hasPermi('configManager:identity:remove')")
    @Log(title = "身份", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(mgIdentityService.deleteMgIdentityByIds(ids));
    }
}
